package com.dingding.order.processer.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.common.core.base.daox.IBaseDaoX;
import com.dingding.common.core.beans.special.Specialuserlogin;
import com.dingding.common.core.beans.special.Userspecialorder;
import com.dingding.common.core.beans.special.UserspecialorderT;
import com.dingding.common.core.exception.bussiness.BizExpEnum;
import com.dingding.common.core.exception.bussiness.BussinessException;
import com.dingding.order.core.common.IUserOrderDao;
import com.dingding.order.core.history.dao.IHistoryOrderDao;
import com.dingding.order.core.special.dao.ISpecialOrderDao;
import com.dingding.order.dto.history.HistoryOrderDto;
import com.dingding.order.dto.march.underway.UderWayOrderDto;
import com.dingding.order.dto.pay.paymoney.GetThirdPayAndAccountDto;
import com.dingding.order.processer.IOrderProcesser;

/**
 * @Description:专车处理
 * @author fengshuonan
 * @date 2016年8月18日 下午5:28:49
 * @version 叮叮搭车
 * @since 2.4
 */
@Component("specialOrderProcesser")
public class SpecialOrderProcesser implements IOrderProcesser {

	@Resource(name = "specialOrderDao")
	private ISpecialOrderDao specialOrderDao;

	@Resource(name = "specialHistoryOrderDao")
	private IHistoryOrderDao specialHistoryOrderDao;

	@Resource(name = "baseDaoX")
	private IBaseDaoX baseDaoX;

	@Resource(name = "userOrderCommonDao")
	private IUserOrderDao userOrderDao;
	
	@Resource(name = "normalOrderProcesser")
	private IOrderProcesser normalOrderProcesser;

	private Logger log = Logger.getLogger(this.getClass());

	/**
	 * @Description:获取订单状态（专车的）
	 * @return int (0:无此订单 1:待指派 2:待上车(已指派) 3:已上车 4:待支付(已到达) 5:已完成 6:已取消)
	 */
	@Override
	public int getOrderStatus(String orderId) {
		Integer status = null;
		try {
			status = this.specialOrderDao.getOrderStatus(orderId);
		} catch (Exception e) {
			log.error("查询订单状态时，订单为空！SpecialOrderProcesser：getOrderStatus()", e);
		}
		if (status == null) {
			return 0;
		} else {
			return status;
		}
	}

	@Override
	public GetThirdPayAndAccountDto getThirdPayAndAccountPay(BigDecimal accountRemain, String orderId) {

		BigDecimal orderMoney = new BigDecimal(0);

		UserspecialorderT UserspecialorderT = this.specialOrderDao.getById(orderId);
		if (UserspecialorderT == null) {
			// 从正式库中查询
			Userspecialorder zorder = this.baseDaoX.getById(orderId, Userspecialorder.class);
			if (zorder != null && zorder.getOrderStatus() != null) {
				orderMoney = zorder.getTotalMoney();
			}
		} else {
			orderMoney = UserspecialorderT.getTotalMoney();
		}

		GetThirdPayAndAccountDto dto = new GetThirdPayAndAccountDto();
		if (accountRemain.compareTo(orderMoney) < 0) {
			dto.setAccountPayMoney(accountRemain);
			dto.setThirdPayMoney(orderMoney.subtract(accountRemain));
		} else {
			dto.setAccountPayMoney(orderMoney);
			dto.setThirdPayMoney(new BigDecimal(0));
		}

		return dto;
	}

	@Override
	public List<UderWayOrderDto> getUnderWayOrders(String userId, Integer clientType) {
		return this.specialOrderDao.getSpecialUnderWayOrders(userId, clientType);
	}

	@Override
	public List<HistoryOrderDto> getHistoryOrders(String userId, int orderStatus, int currentPage, int pageSize,
			Integer userType) {
		return this.specialHistoryOrderDao.getHistoryOrders(userId, orderStatus, currentPage, pageSize, userType);
	}

	@Override
	public String getCalledUserTell(String orderId, String userId) {

		UserspecialorderT ordert = this.baseDaoX.getById(orderId, UserspecialorderT.class);

		if (ordert == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_ORDER);
		}

		String calledUserId = null;

		// 如果获取被叫电话号码时，用户id为下单的用户id，则获取被叫电话号码为抢单用户id的电话号码
		if (userId.equals(ordert.getPlaceOrderUserId())) {
			calledUserId = ordert.getGrabOrderUserId();
			String telephone = this.getUserTellByUserId(calledUserId);
			return telephone;
		} else {
			calledUserId = ordert.getPlaceOrderUserId();
			String telephone = this.normalOrderProcesser.getUserTellByUserId(calledUserId);
			return telephone;
		}
	}

	@Override
	public String getUserTellByUserId(String userId) {
		
		Specialuserlogin userlogin = this.baseDaoX.getById(userId, Specialuserlogin.class);

		if (userlogin == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_USER);
		}

		return userlogin.getUserName();
	}

	@Override
	public String getCallUserTell(String orderId, String userId) {
		
		UserspecialorderT ordert = this.baseDaoX.getById(orderId, UserspecialorderT.class);

		if (ordert == null) {
			throw new BussinessException(BizExpEnum.NO_THIS_ORDER);
		}
		
		String callTell = null;
		
		//如果发起用户的id为乘客
		if(userId.equals(ordert.getPlaceOrderUserId())){
			callTell = this.normalOrderProcesser.getUserTellByUserId(userId);
		}else{
			callTell = this.getUserTellByUserId(userId);
		}
		
		return callTell;
	}

	/**
	 * @Description:获取用户的历史订单
	 * @param userId 用户id
	 * @author YHQ
	 * @return List<HistoryOrderDto>
	 * @since 2.5.5
	 */
	@Override
	public List<HistoryOrderDto> completeAndCancelOrder(String userId, int currentPage, int pageSize,
			Integer userType) {
		
		return this.specialHistoryOrderDao.completeAndCancelOrder(userId, currentPage, pageSize, userType);
	}

}
